home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 167 / pascal / fulldir.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-08-19  |  5.0 KB  |  172 lines

  1. PROGRAM dir_list ;
  2.  
  3. (* This Program is written in O.S.S Personal Pascal.  It was written by:
  4.    Christopher Reed over the weekend of 6/27/87.  It was written for two
  5.    reasons:
  6.        1. Provide a tool to list all files on a disk.
  7.        2. Demonstrate the recursive properties of Pascal
  8.  
  9.   I think recursion is an extreamly important concept.
  10.   I have added comments where special scope considerations for recursion
  11.     are required.
  12.   These same considerations apply to non-recursive techiniques !
  13.  
  14.   Also, I present here a program format style which I have developed over the
  15.   (all too many) years.
  16.   Of note is the indentation of IF - THEN-ELSE and BEGIN - END
  17.         and REPEAT - WHILE / UNTIL.
  18.   BEGIN and its END, IF and its ELSE (if present) always start at the same
  19.     offset.
  20.   I always have IF and its THEN, WHILE and its DO on the same line.
  21.   BEGIN, END, ELSE and REPEAT are so important that they appear on a line alone
  22.   (The exception to this is the WITH ... BEGIN which appear on the same line.)
  23.   It is important to have control statements formatted consistantly.
  24.   I indent 3 spaces after controling or guarding statements, but only only one
  25.     after data structuring statements.
  26.   I have more style rules but the above are the most important.   *)
  27.  
  28. CONST
  29.    suffex_len = 5 ;
  30.    suffex_value = '\*.*' ;
  31.    attr_node_bit = $10 ;
  32.  
  33. TYPE
  34.   name_inx = 0..79 ;
  35.   path_name = PACKED ARRAY [ name_inx ] OF char ;
  36.   fn_range = 0..13 ;
  37.   fname = PACKED ARRAY [ fn_range ] OF char ;
  38.   frec = PACKED RECORD
  39.      reserved : PACKED ARRAY [ 0..19 ] OF byte ;
  40.      resvd2 : byte ;
  41.      attrib : byte ;
  42.      time_stamp : integer ;
  43.      date_stamp : integer ;
  44.      size : long_integer ;
  45.      name : fname ;
  46.    END ;
  47.   title = record
  48.      length : integer ;
  49.      name   : path_name ;
  50.    END ;
  51.  
  52. VAR
  53.   i : fn_range ;
  54.   path_string : STRING ;
  55.   path : title ;
  56.  
  57. PROCEDURE set_dta( VAR buf : frec ) ;
  58.    GEMDOS( $1a ) ;
  59.  
  60. FUNCTION get_first( var path : path_name ; search_attrib :integer ):integer ;
  61.    GEMDOS( $4e ) ;
  62.  
  63. FUNCTION get_next : integer ;
  64.    GEMDOS( $4f ) ;
  65.  
  66. PROCEDURE show_file( VAR r : frec ) ;
  67.  var
  68.    i : fn_range ;
  69. BEGIN WITH r DO
  70.  BEGIN
  71.    write ( '     '  ) ;
  72.    i := 0;
  73.    WHILE name[i] <> chr(0) DO
  74.    BEGIN
  75.       write ( name[i] );
  76.       i := i + 1;
  77.    END ;
  78.    writeln ;
  79.  END ;
  80. END ;
  81.  
  82. FUNCTION get_path (var path : title ) : boolean ;
  83.  var
  84.    path_string : string ;
  85.    i : integer ;
  86. BEGIN
  87.    get_path :=  false;
  88.    write( 'search path: ' ) ;
  89.    readln( path_string ) ;
  90.    IF length ( path_string ) > 0 THEN
  91.       get_path := true ;
  92.    FOR i := 0 TO length( path_string )-1 DO
  93.       path.name[i] := path_string[i+1] ;
  94.    path.length := length ( path_string );
  95.    path.name[ path.length +1 ] := chr(0) ;
  96. END ;
  97.  
  98. PROCEDURE append ( a : fname ; var b : title ) ;
  99.  var
  100.    i : integer;
  101. BEGIN
  102.    i := 0;
  103.    WITH b DO BEGIN
  104.     name[length] := '\' ;
  105.     length := length + 1 ;
  106.     WHILE a[i] <> chr(0) DO
  107.     BEGIN
  108.        name [ length ] := a[i] ;
  109.        length := length + 1 ;
  110.        i := i + 1 ;
  111.     END ;
  112.     name [ length ] := chr(0) ;
  113.  END ;
  114. END ;
  115.  
  116. PROCEDURE append_stars ( a : title; var b :path_name ) ;
  117.  var
  118.    suffex : string [ suffex_len ] ;
  119.    i : integer ;
  120. BEGIN
  121.    (* appends '\*.*' to a and returns as b*)
  122.    suffex := suffex_value ;
  123.    b := a.name ;
  124.    for i := 0 to suffex_len -1  DO
  125.       b [ i +a.length ] := suffex[i +1] ;
  126.    b [a.length +suffex_len +1] := chr(0) ;
  127. END ;
  128.  
  129. FUNCTION Search_Dir ( path : title ) : boolean ;
  130.  var
  131.    path_param : title ;  { to be used for the recursive call }
  132.    file_name  : path_name ;
  133.    r          : frec ;   (* to hold the results of GEM_DOS calls *)
  134. BEGIN
  135.    writeln (' search for : ', path.name );
  136.    set_dta( r ) ;
  137.    Search_Dir := true ; (* assume there is no file *)
  138.    append_stars ( path, file_name ) ;
  139.    IF get_first( file_name, 1 | attr_node_bit  ) = 0 THEN
  140.    BEGIN
  141.       Search_Dir := false ; (* there is a file here *)
  142.       REPEAT
  143.          IF r.attrib & attr_node_bit <> 0 THEN
  144.          BEGIN (* it's a node but might not be real *)
  145.             IF r.name[0] <> '.'  THEN
  146.             BEGIN (* its a real node, prepare to re-search *)
  147.                   { The var Path-param only is needed in this block but
  148.                     Pascal dosen't offer a new var declaration here
  149.                     ( use MODULA2 ) }
  150.                path_param := path ; { create a new var for next level }
  151.                append ( r.name, path_param );
  152.                IF Search_Dir ( path_param ) THEN { RECURSE }
  153.                   Writeln ( '** No Item in this folder **' ) ;
  154.                {***  RE-ESTABLISH THE SYSTEM GLOBALS AT THIS LEVEL ***}
  155.                set_dta( r ) ;
  156.             END ;
  157.          END
  158.          ELSE
  159.             show_file( r ) ; (* or any other file processing *)
  160.       UNTIL get_next < 0 ;
  161.       writeln ( '<' );
  162.    END ;
  163. END ;
  164.  
  165. BEGIN { DIR.LIST }
  166.    WHILE get_path ( path ) DO
  167.    BEGIN
  168.       IF Search_Dir ( path ) THEN
  169.          writeln( 'no files match specification!' ) ;
  170.    END ;
  171. END.
  172.